package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsCatalogOfWork;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:47
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsCatalogOfWorkDAOImpl implements DAO<CrumbsCatalogOfWork>{
    @Override
    public void add(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(work);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(work);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsCatalogOfWork getById(int work_id) {
        Session session = null;
        CrumbsCatalogOfWork work = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            work = (CrumbsCatalogOfWork) session.get(CrumbsCatalogOfWork.class, work_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return work;
    }

    @Override
    public Collection<CrumbsCatalogOfWork> getAll() {
        Session session = null;
        List works = new ArrayList<CrumbsCatalogOfWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            works = session.createCriteria(CrumbsCatalogOfWork.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return works;
    }

    @Override
    public void delete(CrumbsCatalogOfWork work) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(work);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
